package com.market.cash.ui.presenter

import com.market.cash.http.ApiService
import com.market.cash.http.ApiService.addCommonParams
import com.market.cash.http.IHttpCallback
import com.market.cash.pro.ProPresenter
import com.market.cash.ui.view.ICashRootVerificationView
import com.market.cash.utils.helper.SharedHelper
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/24 13:45
 * @Annotation：校验 Presenter 实现
 */
class CashRootVerificationPresenter(private val iView: ICashRootVerificationView) : ProPresenter(iView) {

    //选择类型  SMS  、 WHATSAPP
    private var selectType = ""

    fun getSelectType(): String{
        return selectType
    }

    /**
     * 切换校验类型
     */
    fun checkCashRootVerificationType(type: String){
        selectType = type
        sendCashRootWhatsAppCodeAction(type)
        iView.showCashRootVerificationType(type)
    }

    /**
     * 发送WhatsApp验证码
     */
    private fun sendCashRootWhatsAppCodeAction(sendType: String){
        iView.showCashRootLoading()
        val loginMobile = SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_MOBILE)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.CASHROOT_API_WHATSAPP_SEND_CODE_PATH)
            put("product",iView.loadPName())
            put("token",iView.loadPToken())
            put("mobile", loginMobile)
            put("submitMobile", loginMobile)
            put("sendType",sendType)
        }
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.CASHROOT_API_COMMON_URL)
        doCashRootPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //当为YES时可以跳过，直接调用流程接口，根据流程步骤whatsappFlag标识走
                    val directLoginFlag = obj.optString("directLoginFlag")
                    iView.showCashRootSendCodeSuccess(directLoginFlag)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hideCashRootLoading()
            }
        })
    }

    /**
     * 校验验证码
     */
    fun onCashRootVerificationCodeAction(code: String){
        iView.showCashRootLoading()
        val loginMobile = SharedHelper.getCashRootSPString(SharedHelper.CashRoot_SPKEY.CASHROOT_MOBILE)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.CASHROOT_API_WHATSAPP_CHECK_CODE_PATH)
            put("product",iView.loadPName())
            put("token",iView.loadPToken())
            put("mobile", loginMobile)
            put("submitMobile", loginMobile)
            put("code",code)
            put("sendType",getSelectType())
        }
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.CASHROOT_API_COMMON_URL)
        doCashRootPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.hideCashRootLoading()
                iView.showCashRootVerificationSuccess()
            }

            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hideCashRootLoading()
            }
        })
    }
}